package echat.future.cn.echat.my.view;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import echat.future.cn.echat.R;
import echat.future.cn.echat.databinding.ActivityBlacklistBinding;
import echat.future.cn.echat.my.adapter.BlackListAdapter;
import echat.future.cn.echat.my.api.UserApi;
import echat.future.cn.echat.my.bean.BlackListBean;
import echat.future.cn.echat.my.utils.HttpUtilsRequst;
import echat.future.cn.echat.my.utils.UserHelper;
import echat.future.cn.echat.my.weight.Navagation.AdapterNavagation;
import echat.future.cn.echat.my.weight.Navagation.IBaseNavagation;

/**
 * 黑名单列表
 */
public class BlacklistActivity extends BaseActivity<ActivityBlacklistBinding>{

    BlackListAdapter blackListAdapter;

    private List<BlackListBean> mDatas = new ArrayList<>();

    @Override
    public int layoutId() {
        return R.layout.activity_blacklist;
    }

    @Override
    public IBaseNavagation navagation() {
        return new AdapterNavagation(this).setTitle("黑名单", 16, R.color.white).setBack(R.drawable.btn_back).setNavagationBackgroudColor(R.color.color_161823);
    }

    @Override
    public void initView() {
        blackListAdapter = new BlackListAdapter(mDatas);
        bind.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        bind.recyclerView.setAdapter(blackListAdapter);
        loadMore(1);
        blackListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if(view.getId() == R.id.delete){
                    remove(mDatas.get(position).getBlack_id());
                }
            }
        });
        blackListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                    page = page +1;
                    loadMore(page);
            }
        },bind.recyclerView);
    }

    private void remove(int id){
        new HttpUtilsRequst<String>(this,HttpUtilsRequst.retrofitClient.create(UserApi.class).removeBlackList(UserHelper.getToken(),id))
                .setmOnRequstSuccessListener(o -> {
                    toast("移除黑名单成功");
                    page = 1;
                    loadMore(page);
                })
                .start();
    }

    int page = 1;
    private void loadMore(final int pgae){
        new HttpUtilsRequst<>(this,HttpUtilsRequst.retrofitClient.create(UserApi.class).balckList(
                UserHelper.getToken(),pgae
        )).setmOnRequstSuccessListener((HttpUtilsRequst.OnRequstSuccessListener<List<BlackListBean>>) o -> {
            if(pgae == 1){
                mDatas.clear();
            }
            blackListAdapter.loadMoreComplete();
            mDatas.addAll(o);
            blackListAdapter.notifyDataSetChanged();
            if(o.size()<10){
                blackListAdapter.loadMoreEnd();
            }
        }).start();

        this.blackListAdapter.notifyDataSetChanged();
    }
}
